package com.cyt.sys.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.model.vo.SelectVo;
import com.cyt.sys.dao.SysOrgUserDao;
import com.cyt.sys.dao.SysUserDao;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.po.SysOrgUser;
import com.cyt.sys.model.vo.SysOrgUserVo;
import com.cyt.sys.model.vo.SysUserVo;
import com.cyt.sys.service.SysOrgUserService;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 人员组织service层
 *
 * @author shengQiang yu
 * @date 2019-01-29 15:03
 */
@Service("baseOrgUserService")
public class SysOrgUserServiceImpl extends BaseServiceImpl<SysOrgUser> implements SysOrgUserService {
    @Resource
    private SysOrgUserDao sysOrgUserDao;
    @Resource
    private SysUserDao sysUserDao;

    @Autowired
    public void setBaseDao(SysOrgUserDao sysOrgUserDao) {
        this.baseDao = sysOrgUserDao;
    }
    /**
     * 人员选择器封装
     *
     * @param sysUserVo 对象
     * @return ReturnResult 对象
     * @author shengQiang yu
     * @date 2019-04-25 9:03
     */
    public ReturnResult selector(SysUserVo sysUserVo){
        List<SysUser> list= sysUserDao.listLeftOrg(sysUserVo);
        List<SelectVo> selectVoList=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for(SysUser sysUser :list){
                List<String> showNameList=new ArrayList<>();
                showNameList.add(sysUser.getName());
                showNameList.add(sysUser.getOrgName());
                selectVoList.add(new SelectVo(sysUser.getId(), sysUser.getName(),showNameList));
            }
        }
        return new ReturnResult(selectVoList);
    }

    /**
     * 获取组织用户中用户信息
     *
     * @param sysOrgUserVo 对象
     * @return list BaseOrgUser对象集合
     * @author shengQiang yu
     * @date 2019-04-25 9:03
     */
    public List<SysOrgUser> listLeftUser(SysOrgUserVo sysOrgUserVo){
        return sysOrgUserDao.listLeftUser(sysOrgUserVo);
    }

    /**
     * 获取组织用户中组织信息
     *
     * @param sysOrgUserVo 对象
     * @return list BaseOrgUser对象集合
     * @author shengQiang yu
     * @date 2019-04-25 9:03
     */
    public List<SysOrgUser> listLeftOrg(SysOrgUserVo sysOrgUserVo){
        return sysOrgUserDao.listLeftOrg(sysOrgUserVo);
    }
    /**
     * 保存或更新组织信息
     *
     * @param sysOrgUserVo
     * @author changZheng hou
     * @date 2019/4/4 14:21
     */
    @Override
    public void synSave(SysOrgUserVo sysOrgUserVo) {
        sysOrgUserDao.deleteByUserId(sysOrgUserVo.getUserId());
        if (StringUtil.isNotEmpty(sysOrgUserVo.getUserId())) {
            if (StringUtil.isNotEmpty(sysOrgUserVo.getOrgId())) {
                String[] ids = sysOrgUserVo.getOrgId().split(",");
                for (String id : ids) {
                    sysOrgUserDao.insert(new SysOrgUser(sysOrgUserVo.getUserId(), id));
                }
            }
        }
    }

    /**
     * 查询对象集合
     *
     * @param sysOrgUserVo 对象
     * @return
     * @author shengQiang yu
     * @date 2019-04-23 17:04
     */
    public List<SysOrgUser> listExample(SysOrgUserVo sysOrgUserVo) {
        Example example = new Example(SysOrgUser.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty( sysOrgUserVo.getOrgId())) {
            criteria.andEqualTo("orgId", sysOrgUserVo.getOrgId());
        }
        if (StringUtil.isNotEmpty( sysOrgUserVo.getUserId())) {
            criteria.andEqualTo("userId", sysOrgUserVo.getUserId());
        }
        List<SysOrgUser> list = sysOrgUserDao.selectByExample(example);
        return list;
    }

    @Override
    public void delAll() {
        sysOrgUserDao.delAll();
    }

}
